#ifndef LIBCOM_SRC_DATA_H
#define LIBCOM_SRC_DATA_H

#include <inttypes.h>
#include <memory>
#include <stdio.h>
#include <string>
#include <sys/time.h>
#include <time.h>
#include <utils/logger.h>
#include <utils/macro.h>
#include <vector>

#define TRANS_GET_LEN(d) (strlen(d))
#define TRANS_GET_V16(p) (((p)[0] << 8) | (p)[1])
#define TRANS_GET_V32(p) ((p)[0] << 24 | (p)[1] << 16 | (p)[2] << 8 | (p)[3])
#define TRANS_PRINT(p, len)                         \
    {                                               \
        char *info = (char *)malloc(3 * len + 1);   \
        for (int i = 0; i < len; i++) {             \
            sprintf(&info[3 * i], "%02X ", (p)[i]); \
        }                                           \
        info[3 * len] = '\0';                       \
        LOG_DEBUG("%s", info);                      \
        free(info);                                 \
    }                                               \
    while (0)

namespace libxcom {

typedef enum {
    TYPE_NONE = 0,
    TYPE_U8,
    TYPE_S8,
    TYPE_U16,
    TYPE_S16,
    TYPE_U32,
    TYPE_S32,
    TYPE_U64,
    TYPE_S64,
    TYPE_F32,
    TYPE_F64,
    TYPE_STR,
    TYPE_POINTER,
    TYPE_CMD,
    TYPE_ARRAY
} TYPE_DEFINE;

typedef struct {
    TYPE_DEFINE type;
    union {
        int8_t s8;
        uint8_t u8;
        int16_t s16;
        uint16_t u16;
        int32_t s32;
        uint32_t u32;
        int64_t s64;
        uint64_t u64;
        float f32;
        double f64;
        char str[8];
    } data;
} UnionData;

#define floatcmp(f, d) ((f - d) < 0.000001 && (f - d) > -0.000001)
// 状态值定义
#define RECV_COMPLETE       0
#define RECV_NOT_COMPLETE   1
#define RECV_NOT_AVAILIABLE 2
#define RECV_FAILED         3
typedef int8_t RECV_RESULT;

class OpValue;
class Express;
class ParseOperator;
class OpInterface;

class OpInterface {
public:
    virtual std::shared_ptr<Express> LocateFunction(const std::string &def, int funcId = -1) = 0;
    virtual OpValue GetVar(const std::string &var) = 0;
};

#define AT_VALUE(pos, len) ((pos) * 1000 + (len))
} // namespace libxcom
#endif // LIBCOM_SRC_DATA_H
